/* 新增库存 */
<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    :bodyStyle="bodyStyle"
    style="top:15%"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" id="customerModal">
        <!-- <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="产品序号">
            <a-input placeholder="请输入唯一序号" v-decorator.trim="['serialNum']" />
          </a-form-item>
        </a-col> -->
        <a-col :span="24 / 2" v-if="noCustomer != true">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户名称">
            <a-input placeholder="请输入客户名称" v-decorator.trim="['customer', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
            <a-input placeholder="请输入名称" v-decorator.trim="['itemName', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="规格">
            <a-input placeholder="请输入规格" v-decorator.trim="['specifications']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="单位">
            <a-input placeholder="请输入单位" v-decorator.trim="['unit']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="数量">
            <a-input placeholder="请输入数量" v-decorator.trim="['quantity', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="单价">
            <a-input placeholder="请输入单价" v-decorator.trim="['price']" />
          </a-form-item>
        </a-col> -->
        <!-- <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="总金额">
            <a-input placeholder="请输入数量" v-decorator.trim="['totalAmount']" />
          </a-form-item>
        </a-col> -->
        <!-- <a-col :span="24 / 2">
          <a-form-item label="统计日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-date-picker
              style="width: 100%"
              v-decorator.trim="['statisticsDate']"
              format="YYYY-MM-DD"
              placeholder="请选择统计日期"
            />
          </a-form-item>
        </a-col> -->
        <a-col :span="24 / 2">
          <a-form-item label="仓库管理人" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入仓库管理人" v-decorator.trim="['depotManager']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2" v-if="showMinStock">
          <a-form-item label="最低库存" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入最低库存" v-decorator.trim="['minStock', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24 / 2">
          <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入" v-decorator.trim="['status']" />
          </a-form-item>
        </a-col> -->
        <a-col :span="24 / 2" v-if="showUnqualifiedStock">
          <a-form-item label="是否不合格" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select
              v-decorator.trim="['unqualifiedStock', validatorRules.name]"
              allowClear
              placeholder="请选择"
              showSearch
              optionFilterProp="children"
            >
              <a-select-option v-for="(item, index) in isOk" :key="index" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="24 / 2">
          <a-form-item label="库存预警" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input placeholder="请输入库存预警" v-decorator.trim="['alert']" />
          </a-form-item>
        </a-col> -->
        <a-col :span="24 / 2">
          <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-textarea placeholder="请输入备注" v-decorator.trim="['remark']" auto-size />
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { addHardware, checkSupplier } from '@/api/api'
import { addStock } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
export default {
  name: 'addStock',
  mixins: [JeecgListMixin],
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      defaultFileList: null,
      isReadOnly: false,
      showUnqualifiedStock: true,
      showMinStock: true,
      isOk: [
        { id: 0, name: '合格' },
        { id: 1, name: '不合格' }
      ],
      bodyStyle: {
        padding: '20',
        height: window.innerHeight * 0.5 + 'px',
        'overflow-y': 'auto'
      },
      labelCol: {
        xs: { span: 24 / 2 },
        sm: { span: 6 }
      },
      url: {
        importExcelUrl: '/systemConfig/upload' + '?' + `biz=bj`
      },
      wrapperCol: {
        xs: { span: 24 / 2 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        name: {
          rules: [
            { required: true, message: '请输入!' }
            // { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
            // { validator: this.validateSupplierName }
          ]
        }
      }
    }
  },
  props: {
    noCustomer: {
      type: Boolean
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}${this.url.importExcelUrl}`
      // return `api${this.url.importExcelUrl}`
    }
  },
  created() {},
  methods: {
    /* 导入 */
    myhandleImportExcel(info) {
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList)
      }
      if (info.file.status === 'done') {
        if (info.file.response) {
          // this.$message.success(`${info.file.name} 文件上传成功`);
          if (info.file.response.code === 200 || info.file.response.code === 10000) {
            this.$message.success(`${info.file.name} 文件上传成功` || info.file.response.data)
            this.defaultFileList = info.file.response.data
            console.log('defaultFileList:', this.defaultFileList)
          } else {
            this.$message.warning(info.file.response.message)
          }
        } else {
          this.$message.error(`${info.file.name} ${info.file.response.data}.`)
        }
      } else if (info.file.status === 'error') {
        this.$message.error(`文件上传失败: ${info.file.msg} `)
      }
    },
    getPage() {
      if (this.$router.currentRoute.name == '顾客财产统计') {
        this.showMinStock = false
      } else {
        this.showMinStock = true
      }
      if (this.$router.currentRoute.name == '顾客财产统计' || this.$router.currentRoute.name == '垫板/工装统计') {
        this.showUnqualifiedStock = false
      } else {
        this.showUnqualifiedStock = true
      }
    },
    add() {
      this.edit()
      console.log(this.$router.currentRoute.name, 'this.$router.currentRoute.name')
    },
    edit(record) {
      this.getPage()
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      console.log(this.model)
      this.$nextTick(() => {
        if (record) {
          var recordDate = this.$moment(this.model.recordDate)
          this.form.setFieldsValue({ recordDate: recordDate })
        }
        this.form.setFieldsValue(
          pick(
            this.model,
            // 'serialNum',
            'minStock',
            'itemName',
            // 'itemNum',
            'specifications',
            'unit',
            'quantity',
            'price',
            // 'totalAmount',
            'depotManager',
            'customer',
            'remark',
            // 'principal',
            'unqualifiedStock'
          )
        )
        autoJumpNextInput('customerModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          console.log(this.model)
          let formData = Object.assign(this.model, values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          if (this.model.id) {
            formData.id = this.model.id
          }
          if (this.model.itemType) {
            formData.itemType = this.model.itemType
          } else if (this.$router.currentRoute.name == '库存统计') {
            formData.itemType = 1
          } else if (this.$router.currentRoute.name == '原材料库存统计') {
            formData.itemType = 2
          } else if (this.$router.currentRoute.name == '贵金属库存统计') {
            formData.itemType = 3
          } else if (this.$router.currentRoute.name == '顾客财产统计') {
            formData.itemType = 4
            formData.unqualifiedStock = 0
          } else if (this.$router.currentRoute.name == '垫板/工装统计') {
            formData.itemType = 5
            formData.unqualifiedStock = 0
          }

          //   formData.recordDate = this.$moment(formData.recordDate).format('YYYY-MM-DD')
          let obj
          if (!this.model.id) {
            obj = addStock(formData)
          } else {
            obj = addStock(formData)
          }
          obj
            .then(res => {
              if (res.code === 200 || res.code === 10000) {
                this.$message.success('保存成功')
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '客户',
        id: this.model.id ? this.model.id : 0
      }
      checkSupplier(params).then(res => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
